package com.android.clinic.doctors.app.ui.patient;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.clinic.doctors.app.ClinicDoctorsApplication;
import com.android.clinic.doctors.app.R;
import com.android.clinic.doctors.app.adapter.BaseListAdapter;
import com.android.clinic.doctors.app.base.BaseRefreshListFragment;
import com.android.clinic.doctors.app.entity.PatientInfo;
import com.android.clinic.doctors.app.glide.ImageLoaderUtils;
import com.android.clinic.doctors.app.http.HttpClient;
import com.android.clinic.doctors.app.http.base.BaseObserver;
import com.android.clinic.doctors.app.http.utils.RxObservableUtils;
import com.android.clinic.doctors.app.utils.Utils;
import com.kennyc.view.MultiStateView;

import java.util.List;

/**
 * 我的患者
 * Created by cheng on 2018/6/20.
 */
public class PatientFragment extends BaseRefreshListFragment<PatientInfo> {

    public static final String PATIENT_TYPE = "";
    private int patientType = 0;

    public static PatientFragment newInstance(int patientType) {
        Bundle bundle = new Bundle();
        PatientFragment fragment = new PatientFragment();
        bundle.putInt(PATIENT_TYPE, patientType);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_refresh_listview;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        super.initView(savedInstanceState);
    }

    @Override
    protected void reloadData() {
        patientType = getArguments().getInt(PATIENT_TYPE);
        if (patientType == 0) {
            getFollowList();
        } else {
            getListEd();
        }
    }

    /**
     * 我的患者
     */
    private void getFollowList() {
        HttpClient.getHttpService().getFollowList(ClinicDoctorsApplication.getUserInfo().getId())
                .compose(RxObservableUtils.applySchedulers())
                .subscribe(new BaseObserver<List<PatientInfo>>() {
                    @Override
                    public void onSuccess(List<PatientInfo> patientInfo) {
                        refreshComplete();
                        if (null != patientInfo && patientInfo.size() > 0) {
                            setMultiStateView(MultiStateView.VIEW_STATE_CONTENT);
                            mAdapter.addAll(patientInfo);
                        } else if (pageNum == 1) {
                            setMultiStateView(MultiStateView.VIEW_STATE_EMPTY);
                        }
                    }

                    @Override
                    public void onFailure(String message) {
                        refreshComplete();
                        showToast(message);
                    }
                });
    }

    /**
     * 已处理的记录
     */
    private void getListEd() {
        HttpClient.getHttpService().getListEd(ClinicDoctorsApplication.getUserInfo().getId())
                .compose(RxObservableUtils.applySchedulers())
                .subscribe(new BaseObserver<List<PatientInfo>>() {
                    @Override
                    public void onSuccess(List<PatientInfo> patientInfo) {
                        refreshComplete();
                        if (null != patientInfo && patientInfo.size() > 0) {
                            setMultiStateView(MultiStateView.VIEW_STATE_CONTENT);
                            mAdapter.addAll(patientInfo);
                        } else if (pageNum == 1) {
                            setMultiStateView(MultiStateView.VIEW_STATE_EMPTY);
                        }
                    }

                    @Override
                    public void onFailure(String message) {
                        refreshComplete();
                        showToast(message);
                    }
                });
    }


    @Override
    protected BaseListAdapter<PatientInfo> getAdapter() {
        return new PatientAdapter(mActivity);
    }

    private class PatientAdapter extends BaseListAdapter<PatientInfo> {

        public PatientAdapter(Context context) {
            super(context, R.layout.item_patient_list);
        }

        @Override
        protected Object setupViewHolder(@Nullable View convertView, ViewGroup parent, int typeIndex) {
            return new ViewHolder(convertView);
        }

        @Override
        protected void setupBindViewHolder(final PatientInfo object, Object viewHolder, int position, int typeIndex) {
            ViewHolder holder = (ViewHolder) viewHolder;
            if (!Utils.isEmpty(object.getNickName())) {
                holder.tvName.setText(object.getNickName());
            } else {
                holder.tvName.setText("--");
            }
            holder.tvPhone.setText(object.getMobile());
            holder.tvTime.setText(patientType == 0 ? Utils.getFormatDateTime(getItem(position).getCreateTime()) : Utils.getFormatTime(getItem(position).getGmtModified()));
            if (!Utils.isEmpty(object.getGender())) {
                holder.ivSex.setImageResource(object.getGender().equals("男") ? R.drawable.icon_male : R.drawable.icon_female);
            }
            ImageLoaderUtils.getInstance().loadHeadImage(object.getImgUrl(), holder.ivHead);

            holder.laySendMessage.setOnClickListener(v -> {
                Bundle bundle = new Bundle();
                bundle.putString("name", object.getNickName());
                bundle.putString("userId", object.getId());
                startToActivity(ReplyPatientActivity.class, bundle);
            });

        }

        class ViewHolder extends BaseViewHolder {

            ImageView ivHead, ivSex;
            TextView tvName, tvTime, tvPhone, tvDefaultName;
            LinearLayout laySendMessage;
            FrameLayout layDefault;

            public ViewHolder(View itemView) {
                super(itemView);
                ivHead = itemView.findViewById(R.id.iv_head);
                ivSex = itemView.findViewById(R.id.iv_sex);
                tvName = itemView.findViewById(R.id.tv_name);
                tvTime = itemView.findViewById(R.id.tv_time);
                tvPhone = itemView.findViewById(R.id.tv_phone);
                laySendMessage = itemView.findViewById(R.id.layout_send_message);
                layDefault = itemView.findViewById(R.id.layout_default);
                tvDefaultName = itemView.findViewById(R.id.tv_default_name);
            }
        }
    }
}
